package com.fsxgt.datagrid.datax;




import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson2.JSON;
import com.fsxgt.datagrid.component.bo.CmpSyncOfflineBO;
import com.fsxgt.datagrid.datax.reader.ReaderBuilder;
import com.fsxgt.datagrid.datax.reader.impl.ClickHouseReader;
import com.fsxgt.datagrid.datax.reader.impl.HiveReader;
import com.fsxgt.datagrid.datax.reader.impl.MysqlReader;
import com.fsxgt.datagrid.datax.reader.impl.OracleReader;
import com.fsxgt.datagrid.datax.reader.impl.PostgresqlReader;
import com.fsxgt.datagrid.datax.reader.impl.SqlServerReader;
import com.fsxgt.datagrid.datax.writer.WriterBuilder;
import com.fsxgt.datagrid.datax.writer.impl.ClickHouseWriter;
import com.fsxgt.datagrid.datax.writer.impl.HiveWriter;
import com.fsxgt.datagrid.datax.writer.impl.MysqlWriter;
import com.fsxgt.datagrid.datax.writer.impl.OracleWriter;
import com.fsxgt.datagrid.datax.writer.impl.PostgresqlWriter;
import com.fsxgt.datagrid.datax.writer.impl.SqlServerWriter;
import com.fsxgt.datagrid.ds.component.QueryComponent;
import com.fsxgt.datagrid.ds.utils.JdbcConstants;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

/**
 * 构建 com.wugui.datax json的工具类
 *
 * @author jingwk
 * @ClassName DataxJsonHelper
 * @Version 2.1.1
 * @since 2020/03/14 08:24
 */
@Component
public class DataxJsonHelper  {
	
	@Autowired
	private ClickHouseWriter clickHouseWriter;
	@Autowired
	private ClickHouseReader clickHouseReader;
	@Autowired
	private OracleReader oracleReader;
	@Autowired
	private OracleWriter oracleWriter;
	@Autowired
	private MysqlWriter mysqlWriter;
	@Autowired
	private MysqlReader mysqlReader;
	@Autowired
	private SqlServerWriter sqlServerWriter;
	@Autowired
	private SqlServerReader sqlServerReader;
	@Autowired
	private PostgresqlReader postgresqlReader;
	@Autowired
	private PostgresqlWriter postgresqlWriter;
	@Autowired
	private QueryComponent queryComponent;
	
	@Autowired
	private HiveReader hiveReader;
	@Autowired
	private HiveWriter hiveWriter;
	

	

	private ReaderBuilder getReader(String dbType) throws ExecutionException {


		if (JdbcConstants.MYSQL.equals(dbType)) {
            return mysqlReader;
        }  else if (JdbcConstants.SQL_SERVER.equals(dbType)) {
            return sqlServerReader;
        }  else if (JdbcConstants.CLICKHOUSE.equals(dbType)) {
            return clickHouseReader;
        }else if (JdbcConstants.ORACLE.equals(dbType)) {
            return oracleReader;
        }else if (JdbcConstants.POSTGRESQL.equals(dbType)) {
            return postgresqlReader;
        }else if (JdbcConstants.HIVE.equals(dbType)) {
            return hiveReader;
        }
		
		return null; 
		
	}
	
	private WriterBuilder getWriter(String dbType) throws ExecutionException {
		

		if (JdbcConstants.MYSQL.equals(dbType)) {
            return mysqlWriter;
        }  else if (JdbcConstants.SQL_SERVER.equals(dbType)) {
            return sqlServerWriter;
        }  else if (JdbcConstants.CLICKHOUSE.equals(dbType)) {
            return clickHouseWriter;
        }else if (JdbcConstants.ORACLE.equals(dbType)) {
            return oracleWriter;
        }else if (JdbcConstants.POSTGRESQL.equals(dbType)) {
            return postgresqlWriter;
        }else if (JdbcConstants.HIVE.equals(dbType)) {
            return hiveWriter;
        }
		
		return null; 
		
	}
    

    

 
    public Map<String, Object> buildJSON(CmpSyncOfflineBO bo, Map params) throws Exception {
    	
    	String srcDbType = queryComponent.getDatasource(bo.getSrcDatasourceId()).getDatasource();
    	String targetDbType = queryComponent.getDatasource(bo.getTargetDatasourceId()).getDatasource();

    	
        Map<String, Object> res = Maps.newLinkedHashMap();
        Map<String, Object> jobMap = Maps.newLinkedHashMap();
        jobMap.put("setting", buildSetting(bo));
        jobMap.put("content", ImmutableList.of(buildContent(srcDbType,targetDbType,bo,params)));
        res.put("job", jobMap);
        res.put("core", JSON.parse("{\"transport\": {\"channel\": {\"speed\": {\"byte\": 1231412}}}}"));
        return res;
    }

   
    private Map<String, Object> buildSetting(CmpSyncOfflineBO bo) {
        Map<String, Object> res = Maps.newLinkedHashMap();
        Map<String, Object> speedMap = Maps.newLinkedHashMap();
        Map<String, Object> errorLimitMap = Maps.newLinkedHashMap();
        speedMap.putAll(ImmutableMap.of("channel", bo.getThreads()!=null?Integer.valueOf(bo.getThreads()):1, "byte", 1048576));
       
        Float maxErrorPercent=0.01f;
        
        if(bo.getMaxErrorPercent()!=null) {
        	maxErrorPercent=Float.valueOf(bo.getMaxErrorPercent());
        	maxErrorPercent=Float.valueOf(String.format("%.2f", maxErrorPercent/100));
        }
        
        errorLimitMap.putAll(ImmutableMap.of("record", bo.getMaxErrorCount()!=null?Integer.valueOf(bo.getMaxErrorCount()):10, "percentage", 
        		maxErrorPercent));
        res.put("speed", speedMap);
        res.put("errorLimit", errorLimitMap);
        return res;
    }


    private Map<String, Object> buildContent(String srcDbType,String targetDbType,CmpSyncOfflineBO bo, Map params) throws Exception {

        Map<String, Object> res = Maps.newLinkedHashMap();
        res.put("reader", this.getReader(srcDbType).build(bo,params));
        res.put("writer", this.getWriter(targetDbType).build(bo,params));
        
        

        return res;
    }

    public static void main(String args[]) {
    	Float maxErrorPercent = Float.valueOf("33");
    	
    	maxErrorPercent=Float.valueOf(String.format("%.2f", maxErrorPercent/100));
    	
    	System.out.println(maxErrorPercent);
    }

    
    

   





}
